package com.hiray.sample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.hiray.calendar.CalendarMonthView;
import com.hiray.calendar.CalendarUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    List<YearMonth> data = new ArrayList<>();
    private int index;
    private LinearLayoutManager linearLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recyclerview);
        init();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.jump, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.vertical) {
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        } else {
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        }
        return super.onOptionsItemSelected(item);
    }

    private void init() {
        loadData();
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        linearLayoutManager.scrollToPosition(index);
        recyclerView.setAdapter(new RecyclerView.Adapter<CalendarViewHolder>() {

            private LayoutInflater layoutInflater;

            @Override
            public CalendarViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                layoutInflater = LayoutInflater.from(MainActivity.this);
                return new CalendarViewHolder(layoutInflater.inflate(R.layout.item_calendar_month,
                        parent, false));
            }

            @Override
            public void onBindViewHolder(CalendarViewHolder holder, int position) {
                YearMonth yearMonth = data.get(holder.getAdapterPosition());
                holder.monthView.set(yearMonth.year, yearMonth.month);
            }

            @Override
            public int getItemCount() {
                return data.size();
            }
        });
        LinearSnapHelper snapHelper = new LinearSnapHelper();
        snapHelper.attachToRecyclerView(recyclerView);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
                    YearMonth ym = data.get(firstVisibleItemPosition);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }

    class CalendarViewHolder extends RecyclerView.ViewHolder {
        CalendarMonthView monthView;

        public CalendarViewHolder(View itemView) {
            super(itemView);
            monthView = (CalendarMonthView) itemView;
            monthView.setOnDateClickListener(new CalendarMonthView.OnDateClickListener() {
                @Override
                public void onDateClick(int year, int month, int date) {
                    setTitle("Date:" + year + "-" + (month + 1) + "-" + date);
                }
            });
        }
    }

    private void loadData() {
        int y = CalendarUtil.year();
        int m = CalendarUtil.month();
        int n = 0;
        for (int year = 1900; year < 2050; year++) {
            for (int month = 0; month < 12; month++) {
                data.add(new YearMonth(year, month));
                if (y == year && m == month)
                    index = n;
                n++;
            }
        }
    }
}
